package com.controller;

import com.bean.Staffs;
import com.dao.Impl.StaffsDaoImpl;
import com.dao.StaffsDao;
import com.util.GsonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 第一：定义一个类，继承HttpServlet
 * 第二：重写doXxx方法，实现处理客户端浏览器发起的Xxx请求
 * 第三：在doXxx方法中，编写具体代码，实现具体的业务逻辑（功能）
 * 第四：配置请求URL,暴露服务器程序的访问地址
 *
 * @Author Cc
 * @Date 2024-03-11
 */
@WebServlet("/GetAllStaff")
public class ListAllStaffServlet extends HttpServlet {
    final StaffsDao DAO = new StaffsDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        List<Staffs> staffs = DAO.selectAll(1,5);
        String json = GsonUtil.toJson(staffs);

        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}
